function bubbleSort (arr) {
    // 记录最后一次交换的位置
    let lastExchangeIndex = 0
    // 无序数列的边界，每次比较只需要比到这里为止
    let sortBorder = arr.length - 1
    for (let i = 0; i < arr.length - 1; i++) {
        // 有序标记，每一轮的初始值都是true    
        let isSorted = true
        for (let j = 0; j < sortBorder; j++) {
            let tmp = 0
            if (arr[j] > arr[j + 1]) {
                tmp = arr[j]
                arr[j] = arr[j + 1]
                arr[j + 1] = tmp
                isSorted = false
                lastExchangeIndex = j
            }
        }
        sortBorder = lastExchangeIndex
        if (isSorted) {
            break;
        }
    }
}
let arr = [2, 1, 3, 4, 5, 6, 7, 8]
bubbleSort(arr)
console.log(arr)